/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.agro.totales.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.marcelo.agro.totales.gui//AreaChart//EN",
autostore = false)
@TopComponent.Description(preferredID = "AreaChartTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "org.marcelo.agro.totales.gui.AreaChartTopComponent")
@ActionReference(path = "Menu/Reportes" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_AreaChartAction",
preferredID = "AreaChartTopComponent")
@Messages({
    "CTL_AreaChartAction=AreaChart",
    "CTL_AreaChartTopComponent=AreaChart Window",
    "HINT_AreaChartTopComponent=This is a AreaChart window"
})
public final class AreaChartTopComponent extends TopComponent {

    private javax.swing.Timer tiempo ;
    private AreaChart<Number,Number> areaChart;
    private JFXPanel jFXPanel;
    private void initJpanel() {
        jPanel2.setLayout(new BorderLayout());
    }
    
    protected AreaChart<Number, Number> createAreaChart() {
            NumberAxis xAxis = new NumberAxis();
            NumberAxis yAxis = new NumberAxis();
            AreaChart<Number,Number> ac = new AreaChart<Number,Number>(xAxis,yAxis);
            // setup chart
            ac.setTitle("Ejemplo Clima");
            xAxis.setLabel("Meses");
            yAxis.setLabel("Precipitaciones en mm");
            // add starting data
            for (int s=0;s<3;s++) {
                    XYChart.Series<Number,Number> series = new XYChart.Series<Number,Number>();
                    series.setName("Serie datos: "+s);
                    double x = 0;
                    while (x<11) {
                        series.getData().add(new XYChart.Data<Number,Number>(x, Math.random()*99));
                        x += 2 + (3*Math.random());
                    }
                    series.getData().add(new XYChart.Data<Number,Number>(11d, Math.random()*99));
                    ac.getData().add(series);
            }
            return ac;
    }
//    private JFXPanel areaPanel;
//    private Scene scene;
    private void initArea() {
        jFXPanel = new JFXPanel();
        Platform.setImplicitExit(false);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                areaChart = createAreaChart();
                Scene scene = new Scene(areaChart);
                jFXPanel.setScene(scene);
            }
        
        });
    }
   
    private void initTimer() {
        tiempo = new javax.swing.Timer(10000, new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent e) {
                Platform.runLater(new Runnable() {
                @Override
                    public void run() {
                      areaChart.getData().clear();
                      areaChart.getData().addAll(createAreaChart().getData());  
                }});
                
            }
        });
    }
    public AreaChartTopComponent() {
        initComponents();
        setName(Bundle.CTL_AreaChartTopComponent());
        setToolTipText(Bundle.HINT_AreaChartTopComponent());
        initJpanel();
        initTimer();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField1 = new javax.swing.JTextField();
        jSlider1 = new javax.swing.JSlider();
        jSlider2 = new javax.swing.JSlider();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 373, Short.MAX_VALUE)
        );

        jTextField1.setText(org.openide.util.NbBundle.getMessage(AreaChartTopComponent.class, "AreaChartTopComponent.jTextField1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 172, Short.MAX_VALUE)
                        .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSlider2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JSlider jSlider2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
        initArea();
        jPanel2.add(jFXPanel,BorderLayout.CENTER);
        tiempo.start();
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
        jPanel2.remove(jFXPanel);
        tiempo.stop();
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}
